package com.iven.xie.redis.controller;

import com.iven.xie.redis.beans.Actor;
import com.iven.xie.redis.beans.ResObject;
import com.iven.xie.redis.util.SerializationUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author iven.xie
 */
@Api(tags = "redis基于jedis的基础操作实例接口")
@RestController
@RequestMapping("api/jedis")
public class RedisJedisController {

    @Autowired
    JedisPool pool;

    @ApiOperation("string-获取字符串类型的值")
    @GetMapping(value = "/string")
    @ResponseBody
    public ResObject<String> getString(@RequestParam String key) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        String value = jedis.get(key);
        jedis.close();
        return ResObject.success(value);
    }

    @ApiOperation("string-写入字符串类型的值")
    @PutMapping(value = "/string")
    @ResponseBody
    public ResObject setString(@RequestParam String key, @RequestParam String value) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        String result = jedis.set(key, value);
        jedis.close();
        return ResObject.success(result);
    }

    @ApiOperation("map-读取hashmap的值")
    @GetMapping(value = "/getmap")
    @ResponseBody
    public ResObject getMap(@RequestParam String key) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        Map<String, String> map = jedis.hgetAll(key);
        jedis.close();
        return ResObject.success(map);
    }

    @ApiOperation("map-向hashmap添加值")
    @PostMapping(value = "/putmap")
    @ResponseBody
    public ResObject getMap(@RequestParam String key, @RequestParam String field, @RequestParam String value) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        Long result = jedis.hset(key, field, value);
        jedis.close();
        return ResObject.success(result);
    }

    @ApiOperation("map-向hashmap删除值")
    @DeleteMapping(value = "/delmap")
    @ResponseBody
    public ResObject delMap(@RequestParam String key, @RequestParam String field) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        Long result = jedis.hdel(key, field);
        jedis.close();
        return ResObject.success(result);
    }

    @ApiOperation("list-读取list的值")
    @GetMapping(value = "/getList")
    @ResponseBody
    public ResObject getList(@RequestParam String key) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        List<String> list = jedis.lrange(key, 0, -1);
        jedis.close();
        return ResObject.success(list);
    }

    @ApiOperation("list-给list添加元素")
    @GetMapping(value = "/addList")
    @ResponseBody
    public ResObject addList(@RequestParam String key, @RequestParam String[] value) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        Long result = jedis.rpush(key, value);
        jedis.close();
        return ResObject.success(result);
    }

    @ApiOperation("list-移除list指定值的元素")
    @GetMapping(value = "/removeList")
    @ResponseBody
    public ResObject removeList(@RequestParam String key, @RequestParam String value) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        Long result = jedis.lrem(key, 0, value);
        jedis.close();
        return ResObject.success(result);
    }

    @ApiOperation("set-读取set的值")
    @GetMapping(value = "/getSet")
    @ResponseBody
    public ResObject getSet(@RequestParam String key) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        Set<String> set = jedis.smembers(key);
        jedis.close();
        return ResObject.success(set);
    }

    @ApiOperation("set-给set添加元素")
    @GetMapping(value = "/putSet")
    @ResponseBody
    public ResObject putSet(@RequestParam String key, @RequestParam String[] value) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        Long result = jedis.sadd(key, value);
        jedis.close();
        return ResObject.success(result);
    }

    @ApiOperation("set-给set删除元素")
    @GetMapping(value = "/delSet")
    @ResponseBody
    public ResObject delSet(@RequestParam String key, @RequestParam String[] value) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        Long result = jedis.srem(key, value);
        jedis.close();
        return ResObject.success(result);
    }

    @ApiOperation("set-读取有序set的值")
    @GetMapping(value = "/getSortedSet")
    @ResponseBody
    public ResObject getSortedSet(@RequestParam String key) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        Set<String> set = jedis.zrange(key, 0, -1);
        jedis.close();
        return ResObject.success(set);
    }

    @ApiOperation("set-删除有序set的值")
    @GetMapping(value = "/delSortedSet")
    @ResponseBody
    public ResObject delSortedSet(@RequestParam String key, @RequestParam String value) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        Long result = jedis.zrem(key, value);
        jedis.close();
        return ResObject.success(result);
    }

    @ApiOperation("set-添加有序set的值")
    @GetMapping(value = "/addSortedSet")
    @ResponseBody
    public ResObject addSortedSet(@RequestParam String key, @RequestParam Double score, @RequestParam String value) {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        Long result = jedis.zadd(key, score, value);
        jedis.close();
        return ResObject.success(result);
    }

    @ApiOperation("string-用string类型写入对象")
    @PostMapping(value = "/addObject")
    @ResponseBody
    public ResObject addObject(@RequestParam String key, @RequestBody Actor actor) throws Exception {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        byte[] bytes = SerializationUtils.serialize(actor);
        String result = jedis.set(key.getBytes(), bytes);
        jedis.close();
        return ResObject.success(result);
    }

    @ApiOperation("string-读取string类型中的对象")
    @GetMapping(value = "/getObject")
    @ResponseBody
    public ResObject getObject(@RequestParam String key) throws Exception {
        Jedis jedis = pool.getResource();
        jedis.select(0);
        byte[] actor = jedis.get(key.getBytes());
        Actor r = SerializationUtils.deserialize(actor, Actor.class);
        jedis.close();
        return ResObject.success(r);
    }

}